﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QN_DiemSV.Forms
{
    public partial class frmDSV : Form
    {
        public frmDSV()
        {
            InitializeComponent();
        }
        bool them = false;
        bool sua = false;
        Classes.connect conn = new QN_DiemSV.Classes.connect();
        void LoadData()
        {
            DataTable dt = new DataTable();
            dt = conn.excuteSelect("select * from DiemMH");
            dgrDiem.DataSource = dt;
        }
        void VisionControl()
        {
            txtDiem.Enabled = true;
            txtHodem.Enabled = true;
            txtTen.Enabled = true;
            txtMaSV.Enabled = true;
        }
        void EnableControl()
        {
            txtDiem.Enabled = false;
            txtHodem.Enabled = false;
            txtTen.Enabled = false;
            txtMaSV.Enabled = false;
        }
        void LoadToCombobox(ComboBox cbo, string sql)
        {
            DataTable dt = conn.excuteSelect(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cbo.Items.Add(dt.Rows[i][0].ToString());
            }
        }
        private void btnEdit_Click(object sender, EventArgs e)
        {
            sua = true;
            VisionControl();
        }

        private void frmDSV_Load(object sender, EventArgs e)
        {
            EnableControl();
            LoadToCombobox(cboMonHoc, "select TenMH from MONHOC");
        }

        private void cboMonHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = conn.excuteSelect("select sv.MaSV, sv.Hodem, sv.Ten, d.DiemMH from SINHVIEN sv left join DIEMMH d on sv.MaSV = d.MaSV where d.MaMH =(select MaMH from MONHOC where TenMH =N'" + cboMonHoc.SelectedItem.ToString() + "')");
            dgrDiem.DataSource = null;
            dgrDiem.DataSource = dt;
        }

        private void dgrDiem_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = e.RowIndex;
            txtMaSV.Text = dgrDiem.Rows[index].Cells["MaSV"].Value.ToString();
            txtHodem.Text = dgrDiem.Rows[index].Cells["Hodem"].Value.ToString();
            txtTen.Text = dgrDiem.Rows[index].Cells["Ten"].Value.ToString();
            txtDiem.Text = dgrDiem.Rows[index].Cells["DiemMH"].Value.ToString();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (sua==true)
            {
                VisionControl();
                conn.excuteNonquery("update DiemMH set DiemMH=" + float.Parse(txtDiem.Text) + " where MaSV ='" + txtMaSV.Text + "'");
                LoadData();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            conn.excuteNonquery("delete from DiemMH where MaSV='" + txtMaSV.Text + "'");
            LoadData();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {

        }

    }
}
